let prices = [7,1,5,3,6,4]

let maxProfit = function(prices){
    let minPrice = Infinity;
    let maxProfit = 0;
    for(let i = 0; i < prices.length; i++){
        // 如果当前价格比最低价格更低，更新最低价格
        if(prices[i] < minPrice){
            minPrice = prices[i]
        }else{
            let profit = prices[i] - minPrice
            if (profit > maxProfit) {
                maxProfit = profit;
            }
        }
    }
    return maxProfit
}

console.log(maxProfit(prices))